// 用于存放表格布局相关方法
import { getUuiKey } from "./index";

// 默认单元格
export function defaultCell() {
  return {
    id: 'td_' + getUuiKey(),
    width: 25,
    colspan: 1,
    rowspan: 1,
    style: {},
    children: [],
    allowDrop: true, // 拖拽时判定目标节点能否被放置
  }
}

// 创建新单元格
export function createCell(num = 1) {
  let array = []
  for (let index = 0; index < num; index++) {
    array.push(defaultCell())
  }
  return array
}

// 合并单元格，得到一个新的单元格,keyName为不变的值
export function mergeCells(array, keyName) {
  // 初始化一个空对象作为合并后的结果
  const mergedObject = { id: getUuiKey(), style: array[0].style || {}, width: 0, colspan: 0, rowspan: 0, children: [], allowDrop: true };
  // 遍历数组中的每个对象
  array.forEach(item => {
    if (item) {
      mergedObject.children = mergedObject.children.concat(item.children)
      mergedObject.colspan += item.colspan;
      mergedObject.rowspan += item.rowspan;
      if (keyName == 'rowspan') mergedObject.width += item.width;
      else mergedObject.width = array[0].width
    }
  });
  if (keyName)
    mergedObject[keyName] = array[0][keyName]
  return mergedObject;
}

// 拆分单元格,按照属性keyName拆分，keyName为rowspan/colspan
export function splitCell(original, keyName = 'colspan') {
  // 确保num可以被整除
  if (original[keyName] % 1 !== 0) {
    throw new Error(keyName + '的值必须能够整除');
  }
  // 创建一个新数组，填充与原对象属性相同的新对象
  const newArray = new Array(original[keyName]).fill(null).map(() => {
    return { ...original };
  });
  // 将新数组中每个对象的num属性设置为1
  newArray.forEach(obj => {
    obj[keyName] = 1;
    obj.children = []
    obj.id = getUuiKey()
    obj.style = original.style || {}
    if (keyName == 'colspan') obj.width = original.width / newArray.length
    else obj.width = original.width
  });
  newArray[0].children = original.children
  return newArray;
}

// 给定 rIndex 和 dIndex 找到距离对应表格左侧最近(包括自身)且不为 null 的表格
export function getLeftTd(list, rIndex, dIndex) {
  let td = null;
  let index = 0;
  for (let i = dIndex; i >= 0; i--) {
    td = list[rIndex].children[i];
    index = i;
    if (td) break;
  }
  return { td, index };
}
// 给定 rIndex 和 dIndex 找到距离对应表格右侧最近(包括自身)且不为 null 的表格
export function getRightTd(list, rIndex, dIndex) {
  const tr = list[rIndex].children;
  let td = null;
  let index = 0;
  for (let i = dIndex; i < tr.length; i++) {
    td = tr[i];
    index = i;
    if (td) break;
  }
  return { td, index };
}

export function getTopTd(list, rIndex, dIndex) {
  let td = null;
  let index = 0;
  for (let i = rIndex; i >= 0; i--) {
    td = list[i].children[dIndex];
    index = i;
    if (td) break;
  }
  return { td, index };
}

export function getBottomTd(list, rIndex, dIndex) {
  let td = null;
  let index = 0;
  for (let i = rIndex; i < list.length; i++) {
    td = list[i].children[dIndex];
    index = i;
    if (td) break;
  }
  return { td, index };
}

// 操作列表
export function defaultOptions(list, rIndex, dIndex) {
  const tr = list[rIndex].children
  const td = tr[dIndex]
  // let topTd, bottomTd, leftTd, rightTd = {};
  // for (let i = rIndex + 1; i < list.length; i++) {
  //   bottomTd = list[i].children[dIndex];
  //   if (bottomTd) break;
  // }
  // for (let i = rIndex - 1; i >= 0; i--) {
  //   topTd = list[i].children[dIndex];
  //   if (topTd) break;
  // }

  // for (let i = dIndex - 1; i >= 0; i--) {
  //   leftTd = list[rIndex].children[i]
  //   if (leftTd) break
  // }
  // for (let i = dIndex + 1; i < tr.length; i++) {
  //   rightTd = tr[i]
  //   if (rightTd) break
  // }
  const { td: leftTd } = getLeftTd(list, rIndex, dIndex - 1)
  const { td: rightTd } = getRightTd(list, rIndex, dIndex + 1)
  const { td: topTd } = getTopTd(list, rIndex - 1, dIndex)
  const { td: bottomTd } = getBottomTd(list, rIndex + 1, dIndex)
  return [
    { value: "leftColumn", label: "左插入列" },
    { value: "rightColumn", label: "右插入列" },
    { value: "topRow", label: "上插入行" },
    { value: "bottomRow", label: "下插入行" },
    {
      divided: true,
      value: "leftMerge",
      label: "向左合并",
      disabled: !(leftTd?.rowspan == td.rowspan)
    },
    {
      value: "rightMerge",
      label: "向右合并",
      disabled: !(rightTd?.rowspan == td.rowspan)
    },
    {
      value: "mergeRow",
      label: "合并整行",
      disabled: !tr.every(item => item?.rowspan === td.rowspan || item === null)
    },
    {
      divided: true,
      value: "topMerge",
      label: "向上合并",
      disabled: !(topTd?.colspan == td.colspan)
    },
    {
      value: "bottomMerge",
      label: "向下合并",
      disabled: !(bottomTd?.colspan == td.colspan)
    },
    {
      value: "mergeColumn",
      label: "合并整列",
      disabled: !list.every(item => (item.children[dIndex]?.colspan == td.colspan || item.children[dIndex] === null))
    },
    {
      divided: true,
      value: "deleteRow",
      label: "删除整行",
      disabled: list.length <= 1 || !tr.every(item => item?.rowspan === td.rowspan || item === null)
    },
    {
      value: "deleteColumn",
      label: "删除整列",
      disabled: list[0].children.length <= 1 || !list.every(item => item.children[dIndex]?.colspan == td.colspan || item.children[dIndex] === null)
    },
    { divided: true, value: "splitRow", label: "拆分成行", disabled: td.rowspan == 1 },
    { value: "splitColumn", label: "拆分成列", disabled: td.colspan == 1 }
  ];
}

export function leftColumn(list, rIndex, dIndex) {
  // 左插入列
  list.forEach(item => {
    item.children.splice(dIndex, 0, defaultCell());
  });
}
export function rightColumn(list, rIndex, dIndex) {
  // 右插入列
  list.forEach(item => {
    item.children.splice(dIndex + 1, 0, defaultCell());
  });
}
export function deleteColumn(list, rIndex, dIndex) {
  // 删除整列
  list.forEach(item => {
    const td = item.children.splice(dIndex, 1);
    const width = td[0].width / item.children.length
    item.children.forEach(td => {
      td.width += width
    })
  });
}
export function topRow(list, rIndex, dIndex) {
  // 上插入行
  const length = list[rIndex].children.length;
  list.splice(rIndex, 0, {
    id: 'tr_' + getUuiKey(),
    children: createCell(length)
  });
}
export function bottomRow(list, rIndex, dIndex) {
  // 下插入行
  const td = list[rIndex].children[dIndex];
  const length = list[rIndex].children.length;
  list.splice(rIndex + td.rowspan, 0, {
    id: 'tr_' + getUuiKey(),
    children: createCell(length)
  });
}
export function deleteRow(list, rIndex, dIndex) {
  // 删除整行
  list.splice(rIndex, 1);
}
export function leftMerge(list, rIndex, dIndex) {
  // 向左合并
  const tr = list[rIndex].children;
  for (let i = dIndex - 1; i >= 0; i--) {
    if (tr[i]) {
      const array = [tr[i], tr[dIndex]];
      const newCell = mergeCells(array, "rowspan");
      tr.splice(i, 1, newCell);
      tr.splice(dIndex, 1, null);
      break;
    }
  }
}
export function rightMerge(list, rIndex, dIndex) {
  // 向右合并
  const tr = list[rIndex].children;
  for (let i = dIndex + 1; i < tr.length; i++) {
    if (tr[i]) {
      const array = [tr[i], tr[dIndex]];
      const newCell = mergeCells(array, "rowspan");
      tr.splice(dIndex, 1, newCell);
      tr.splice(i, 1, null);
      break;
    }
  }
}
export function mergeRow(list, rIndex, dIndex) {
  // 合并整行
  const tr = list[rIndex].children;
  const newCell = mergeCells(tr, "rowspan");
  list[rIndex].children = tr.map(item => null);
  list[rIndex].children.splice(0, 1, newCell);
}
export function
  topMerge(list, rIndex, dIndex) {
  // 向上合并
  for (let i = rIndex - 1; i >= 0; i--) {
    const td = list[i].children[dIndex];
    if (td && td.colspan && td.rowspan) {
      const array = [td, list[rIndex].children[dIndex]];
      const newCell = mergeCells(array, "colspan");
      list[i].children.splice(dIndex, 1, newCell);
      list[rIndex].children.splice(dIndex, 1, null);
      break;
    }
  }
}
export function bottomMerge(list, rIndex, dIndex) {
  // 向下合并
  for (let i = rIndex + 1; i < list.length; i++) {
    const td = list[i].children[dIndex];
    if (td && td.colspan && td.rowspan) {
      const array = [td, list[rIndex].children[dIndex]];
      const newCell = mergeCells(array, "colspan");
      list[rIndex].children.splice(dIndex, 1, newCell);
      list[i].children.splice(dIndex, 1, null);
      break;
    }
  }
}
export function mergeColumn(list, rIndex, dIndex) {
  // 合并整列
  const tds = list.map(item => {
    return item.children[dIndex];
  });
  const newCell = mergeCells(tds, "colspan");
  list.forEach(item => {
    item.children.splice(dIndex, 1, null);
  });
  list[0].children.splice(dIndex, 1, newCell);
}
export function splitRow(list, rIndex, dIndex) {
  // 拆分成行
  const newCells = splitCell(list[rIndex].children[dIndex], "rowspan");
  newCells.forEach((item, i) => {
    list[rIndex + i].children.splice(dIndex, 1, item);
  });
}
export function splitColumn(list, rIndex, dIndex) {
  // 拆分成列
  const newCells = splitCell(list[rIndex].children[dIndex]);
  list[rIndex].children.splice(dIndex, newCells.length, ...newCells);
}
